package 二叉树;

//108 将有序数组转换为二叉搜索树（每次取中来构造，这样就能保证是平衡的搜索树，dfs）
public class Solution16 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return dfs(0,nums.length-1,nums);
    }
    TreeNode dfs(int l, int r, int[] nums){
        if(l>r) return null;
        int mid = (l+r)/2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = dfs(l,mid-1,nums);
        root.right = dfs(mid+1,r,nums);
        return root;
    }
}
